<?php
namespace app\admin\controller;
use app\common\controller\AdminBase;
use think\DB;
/*广告管理*/
class Windows extends AdminBase{
	protected $beforeActionList = [
        'before_index'=>['only'=>'index'],
    ];
	function _initialize() {
		parent::_initialize();  
    }
	function index(){
		$this->assign("now",date("Y-m-d"));
		echo $this->fetch();
	}	
	function delete(){
		$id=input("id");
		db("Ad")->delete($id);
		$this->ajax_success("成功");
	}
	//新增设备
	function addEqui(){
		if(request()->isPost()){
			db("Equipment")->insert(array("mac"=>input("mac"),"name"=>input("name"),"descs"=>input("descs")));
			$this->ajax_success("成功");	
		}else{
			echo $this->fetch();die;		
		}
	}
	function activation(){
		$id=input("id");
		db("Ad")->where("id>0")->setField(array("is_activation"=>0));
		db("Ad")->where("id=$id")->setField(array("is_activation"=>1));
		$event=controller("Api/JpusHelper");
		$model=db("Ad")->where("id=$id")->find();
		$equip_ids=$model['equip_ids'];
		if(empty($equip_ids)){
			$equip_ids="0";
		}
		$macs=db("Equipment")->where("id in($equip_ids)")->column("mac");
		$event->tv($macs,$model['type'],$model["url"]);
		$this->ajax_success("成功");
	}
	/*卡列表数据*/
	function ajax_index(){
		$this->ajax($this->_ajax_index());
	}
	
	function add(){
		if(request()->isPost()){
			$equip_ids=$_POST['equip_ids'];
			if(empty($equip_ids)){
				$equip_ids=[];	
			}
			$url=input("url");
			if(!empty(input("title"))){
				$url=input("title");
			}
			db("Ad")->insert(array("name"=>input("name"),"descs"=>input("descs"),"type"=>input("type"),"url"=>$url,"equip_ids"=>implode(",", $equip_ids)));
			$this->ajax_success("成功");
		}else{
			$equips=db("Equipment")->select();
			if(empty($equips)){
				$equips=[];	
			}
			$this->assign("equips",$equips);
			echo $this->fetch();
		}		
	}
	///////////////////////////////////////////////////////////////////////////////////
	/**
	 * 权限菜单
	 **/
	function before_index(){
		$auth_group=sp_get_auth_menu_3("10041008");
		$this->assign("menus",$auth_group);
	}
	
	/*获取卡数据*/
	private function _ajax_index(){
		$query=array(
				'name'=>array("field" =>"name","operator" =>"like"),
				);
		$draw=input("draw",0)+1;//请求时间		 
		$where_and=join(" and ",sp_get_param_sql(request()->isPost(),$query));
		$total=db("Ad")->where($where_and)->fetchsql(FALSE)->count(1);
		$list=db("Ad")->where($where_and)->order($this->get_order())->limit($this->get_limit())->fetchsql(FALSE)->select();
		
		return array('pageData'=>$list,'total'=>$total,"draw"=>$draw);
	}
}
